<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>单向数据流</h2>
      <button @click="count++">btn</button>
      <child :count="count" :obj="{a: 3, b: 4}"></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const Child = {
        // 子组件里面接收到的props也是具有响应式的
        // 子组件是不能修改接收到的props的值的

        // 如果一定要改，可以将props的值作为data的初始值
        props: {
          count: Number,
          obj: Object,
        },
        data() {
          return {
            num: this.count,
          };
        },
        template: `
          <div>这是child子组件 - {{ num }} - {{ obj }}</div>
          <button @click="fn">btn2</button>
          <button @click="fn2">btn3</button>
          <div>{{ doubleCount }}</div>
        `,
        methods: {
          fn() {
            this.num++;
          },
          fn2() {
            this.obj.a = 30;
            console.log(this.obj);
          },
        },
        computed: {
          doubleCount() {
            return this.count * 2;
          },
        },
      };
      Vue.createApp({
        data() {
          return {
            count: 1,
          };
        },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
